package com.ly.web.controller.erp;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ly.heart.domain.*;
import com.ly.heart.domain.dto.MateriaDto;
import com.ly.heart.domain.dto.MaterialAddDto;
import com.ly.heart.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ly.common.annotation.Log;
import com.ly.common.core.controller.BaseController;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.enums.BusinessType;
import com.ly.common.utils.poi.ExcelUtil;

/**
 * 物料信息Controller
 *
 * @author cxl
 * @date 2024-05-14
 */
@Api(value = "Material", tags = "物料信息")
@RestController
@RequestMapping("/heart/Material")
public class MaterialController extends BaseController {
    @Resource
    private IMaterialService materialEntityService;
    @Resource
    private IMaterialInventoryService materialInventoryService;
    @Resource
    private IMaterialPlanAttributeService materialPlanAttributeService;
    @Resource
    private IMaterialProductService materialProductService;
    @Resource
    private IMaterialProcureService materialProcureService;
    @Resource
    private IMaterialQualityService materialQualityService;
    @Resource
    private IMaterialSaleService materialSaleService;

    /**
     * 查询物料信息列表
     */
//    @PreAuthorize("@ss.hasPermi('heart:Material:list')")
    @GetMapping("/list")
    @ApiOperation("查询物料信息列表")
    public AjaxResult list(MaterialEntity materialEntity) {
        return materialEntityService.selectMaterialEntityListByParentId(materialEntity);
    }

    /**
     * 条件查询，用于各新增页面引用物料，带分页
     *
     * @param materialEntity
     * @return
     */
    @GetMapping("queryList")
    public AjaxResult queryList(MaterialEntity materialEntity) {
        AjaxResult ajaxResult = materialEntityService.queryList(materialEntity);
        return ajaxResult;
    }

    /**
     * 导出物料信息列表
     */
    @PreAuthorize("@ss.hasPermi('heart:Material:export')")
    @Log(title = "物料信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出物料信息列表")
    public void export(HttpServletResponse response, MaterialEntity materialEntity) {
        List<MaterialEntity> list = materialEntityService.list(new LambdaQueryWrapper<MaterialEntity>());
        ExcelUtil<MaterialEntity> util = new ExcelUtil<MaterialEntity>(MaterialEntity.class);
        util.exportExcel(list, "物料信息数据");
    }

    /**
     * 获取物料信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('heart:Material:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取物料信息详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        MaterialEntity material = materialEntityService.getById(id);
        MaterialInventoryEntity inventoryEntity = materialInventoryService.getOne(new LambdaQueryWrapper<MaterialInventoryEntity>().eq(MaterialInventoryEntity::getMaterialId, id));
        if (!ObjectUtils.isEmpty(inventoryEntity)) {
            material.setMaterialInventoryEntity(inventoryEntity);
        }
        MaterialPlanAttributeEntity planAttributeEntity = materialPlanAttributeService.getOne(new LambdaQueryWrapper<MaterialPlanAttributeEntity>().eq(MaterialPlanAttributeEntity::getMaterialId, id));
        if (!ObjectUtils.isEmpty(planAttributeEntity)) {
            material.setMaterialPlanAttributeEntity(planAttributeEntity);
        }
        MaterialProductEntity productEntity = materialProductService.getOne(new LambdaQueryWrapper<MaterialProductEntity>().eq(MaterialProductEntity::getMaterialId, id));
        if (!ObjectUtils.isEmpty(productEntity)) {
            material.setMaterialProductEntity(productEntity);
        }
        MaterialProcureEntity procureEntity = materialProcureService.getOne(new LambdaQueryWrapper<MaterialProcureEntity>().eq(MaterialProcureEntity::getMaterialId, id));
        if (!ObjectUtils.isEmpty(procureEntity)) {
            material.setMaterialProcureEntity(procureEntity);
        }
        MaterialQualityEntity qualityEntity = materialQualityService.getOne(new LambdaQueryWrapper<MaterialQualityEntity>().eq(MaterialQualityEntity::getMaterialId, id));
        if (!ObjectUtils.isEmpty(qualityEntity)) {
            material.setMaterialQualityEntity(qualityEntity);
        }
        MaterialSaleEntity saleEntity = materialSaleService.getOne(new LambdaQueryWrapper<MaterialSaleEntity>().eq(MaterialSaleEntity::getMaterialId, id));
        if (!ObjectUtils.isEmpty(saleEntity)) {
            material.setMaterialSaleEntity(saleEntity);
        }
        return AjaxResult.success(material);
    }

    /**
     * 新增物料信息
     */
//    @PreAuthorize("@ss.hasPermi('heart:Material:add')")
    @Log(title = "物料信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增物料信息")
    public AjaxResult add(@RequestBody MaterialAddDto materialAddDto) {
        return materialEntityService.insertMaterialEntity(materialAddDto);
    }

    /**
     * 修改物料信息
     */
    @PreAuthorize("@ss.hasPermi('heart:Material:edit')")
    @Log(title = "物料信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改物料信息")
    public AjaxResult edit(@RequestBody MaterialEntity materialEntity) {
        return materialEntityService.updateMaterialEntity(materialEntity);
    }

    /**
     * 删除物料信息
     */
    @PreAuthorize("@ss.hasPermi('heart:Material:remove')")
    @Log(title = "物料信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除物料信息")
    public AjaxResult remove(@PathVariable String[] ids) {
        return materialEntityService.deleteMaterialEntityByIds(ids);
    }

    /**
     * 禁用/解禁物料
     * @param id
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('heart:Material:disabled')")
    @Log(title = "物料信息", businessType = BusinessType.UPDATE)
    @GetMapping("/disabled")
    @ApiOperation("禁用/解禁物料")
    public AjaxResult disabled(String id) {
        return materialEntityService.disabled(id);
    }

}
